package com.jeejio.img.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.awesome.img.R
import com.bm.library.PhotoView
import com.jeejio.img.util.ImgLoadUtil.load
import java.util.*

/**
 * Author: JFangZ
 * Email:janezhang1901@163.com
 * Date: 2021/4/26 15:41
 * Description:View big img.
 */
class PhotoViewActivity : AppCompatActivity() {
    private val vpPhotoView: ViewPager by lazy {
        return@lazy findViewById<ViewPager>(R.id.vp_photo_view)
    }
    private val tvIndex: TextView by lazy {
        return@lazy findViewById<TextView>(R.id.tv_index)
    }

    companion object {
        private const val IMG_LIST = "imgList"
        private const val INDEX = "index"

        /**
         * Description:获取跳转到该界面的 Intent
         * Date:2018/3/9
         *
         * @param context   上下文环境
         * @param imageList 要显示的图片列表,元素类型可以是 Bitmap,String 类型的 url,int 类型的资源 Id
         */
        fun start(context: Context, imageList: ArrayList<*>?) {
            start(context, imageList, 0)
        }

        /**
         * Description:获取跳转到该界面的 Intent
         * Date:2018/3/9
         *
         * @param context   上下文环境
         * @param imageList 要显示的图片列表,元素类型可以是 Bitmap,String 类型的 url,int 类型的资源 Id
         * @param index     指定显示第几张图片,默认 0,显示第一张
         */
        fun start(context: Context, imageList: ArrayList<*>?, index: Int) {
            val intent = Intent(context, PhotoViewActivity::class.java)
            intent.putExtra(IMG_LIST, imageList)
            intent.putExtra(INDEX, index)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        StatusBarUtil.setStatusBarColor(window, 0x00000000, true)
//        StatusBarUtil.setStatusBarStyle(window, false)
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
        setContentView(R.layout.activity_photo_view)
        //初始化数据
        var imgList = intent.getStringArrayListExtra(PhotoViewActivity.IMG_LIST)
        val viewList: MutableList<View> = ArrayList()
        if (imgList != null && imgList.isNotEmpty()) {
            for (i in imgList.indices) {
                //创建 PhotoView
                val photoView = PhotoView(this)
                //设置图片
                load(this, imgList.get(i), photoView)
                //开启缩放功能
                photoView.enable()
                //设置图片显示类型
                photoView.scaleType = ImageView.ScaleType.FIT_CENTER
                //点击图片关闭界面
                photoView.setOnClickListener { finish() }
                //长按图片
                photoView.setOnLongClickListener { false }
                //将 PhotoView 添加到容器中
                viewList.add(photoView)
            }
            //初始化下标指示文本
            tvIndex.text = "1/" + imgList.size
        }
        //ViewPager 添加适配器,将 PhotoView 填充进 ViewPager
        vpPhotoView.adapter = object : PagerAdapter() {
            override fun getCount(): Int {
                return viewList.size
            }

            override fun isViewFromObject(view: View, `object`: Any): Boolean {
                return view === `object`
            }

            override fun instantiateItem(container: ViewGroup, position: Int): Any {
                container.addView(viewList[position])
                return viewList[position]
            }

            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
                container.removeView(viewList[position])
            }
        }
        //ViewPager 添加页卡滑动的监听器,动态修改下标指示文本
        vpPhotoView.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
            override fun onPageSelected(position: Int) {
                if (imgList != null && !imgList.isEmpty()) {
                    tvIndex.text = (position + 1).toString() + "/" + imgList.size
                }
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })
        val index = intent.getIntExtra(INDEX, 0)
        if (index >= 0) {
            vpPhotoView.currentItem = index
        }
    }
}